package com.hdoit.ares.logical.acc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.hdoit.ares.logical.acc.dao.IAccountRechargeDao;
import com.hdoit.ares.logical.acc.model.AccountInfo;
import com.hdoit.ares.logical.acc.model.AccountRecharge;
import com.hdoit.ares.logical.acc.model.GoldenBeanDetail;
import com.hdoit.ares.logical.acc.service.IAccountDetailService;
import com.hdoit.ares.logical.acc.service.IAccountInfoService;
import com.hdoit.ares.logical.acc.service.IAccountRechargeService;
import com.hdoit.ares.logical.acc.service.IGoldenBeanDetailService;
import com.hdoit.ares.logical.viewmodel.acc.AccountRechargeSearch;
import com.hdoit.ares.selfcommon.enums.acc.GoldenBeanType;
import com.hdoit.ares.selfcommon.enums.acc.HesitatePeriodState;
import com.hdoit.ares.selfcommon.enums.acc.State;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.logical.setting.dao.IParamOptionDao;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.RandomUtils;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月24日 10:17:14
 * @comment
 */
@Component
public class AccountRechargeServiceImpl extends SuperService<AccountRecharge> implements IAccountRechargeService{
   
	public final static String StrOrderNum = "yyyyMMddHHmmss";
			
	private IAccountRechargeService accountRechargeService;
		
	public IAccountRechargeService getAccountRechargeService() {
		return accountRechargeService;
	}
	
	@Resource
	public void setAccountRechargeService(IAccountRechargeService accountRechargeService) {
		this.accountRechargeService = accountRechargeService;
	}

	private IAccountRechargeDao accountRechargeDao;
	
	public IAccountRechargeDao getAccountRechargeDao() {
		return accountRechargeDao;
	}
    
	@Resource
	public void setAccountRechargeDao(IAccountRechargeDao accountRechargeDao) {
		this.accountRechargeDao = accountRechargeDao;
	}
    
	private IAccountDetailService accountDetailService;
	
	public IAccountDetailService getAccountDetailService() {
		return accountDetailService;
	}
    
	@Resource
	public void setAccountDetailService(IAccountDetailService accountDetailService) {
		this.accountDetailService = accountDetailService;
	}
	
	private IAccountInfoService accountInfoService;
	
	public IAccountInfoService getAccountInfoService() {
		return accountInfoService;
	}
	
	@Resource
	public void setAccountInfoService(IAccountInfoService accountInfoService) {
		this.accountInfoService = accountInfoService;
	}
	
	private IParamOptionDao paramOptionDao;

	public IParamOptionDao getParamOptionDao() {
		return paramOptionDao;
	}
	@Resource
	public void setParamOptionDao(IParamOptionDao paramOptionDao) {
		this.paramOptionDao = paramOptionDao;
	}
	
	private IGoldenBeanDetailService goldenBeanDetailService;
	public IGoldenBeanDetailService getGoldenBeanDetailService() {
		return goldenBeanDetailService;
	}
    @Resource
	public void setGoldenBeanDetailService(IGoldenBeanDetailService goldenBeanDetailService) {
		this.goldenBeanDetailService = goldenBeanDetailService;
	}
	
	
	@Override
	public List<AccountRecharge> queryList(AccountRechargeSearch search, PagerModel pagerModel) {
		return accountRechargeDao.queryList(search,pagerModel);
	}

	@Override
	public List<AccountRecharge> queryList(AccountRechargeSearch search) {
		return accountRechargeDao.queryList(search);
	}

	@Override
	public int queryCount(AccountRechargeSearch search) {
		return accountRechargeDao.queryCount(search);
	}
	
	
	@Override
	public boolean updateByRecharge(String customerId, double money,String outTradeNo) {
		
		ParamOption paramOption = paramOptionDao.queryModelByParamName("exchange");
		int exchangeRate = Integer.valueOf(paramOption.getParamValue());
		double goldBeanNum = exchangeRate * money;
		//第一步:账户金豆增加
		
		AccountInfo accountInfo = accountInfoService.queryModelByCustomerId(customerId);
		accountInfo.setGoldenBean(accountInfo.getGoldenBean() + goldBeanNum);
		accountInfo.setModelStatus(ModelStatus.update);
		
		//第二步:产生充值记录
		AccountRecharge accountRecharge = new AccountRecharge();
		accountRecharge.setId(CommonUtils.getKeyId());
		accountRecharge.setCustomerId(customerId);
		accountRecharge.setRechargeDate(DateUtils.getCurrent());
		accountRecharge.setRechargeMoney(money);
		accountRecharge.setRechagerNum(outTradeNo);
		accountRecharge.setAccountNo(accountInfo.getAccountNo());
		accountRecharge.setDeleteMark(YesOrNo.no);
		accountRecharge.setModelStatus(ModelStatus.add);
		
		//第三步:产生金豆明细
		GoldenBeanDetail goldenBeanDetail = new GoldenBeanDetail();
		goldenBeanDetail.setId(CommonUtils.getKeyId());
		goldenBeanDetail.setCustomerId(customerId);
		goldenBeanDetail.setAccountNo(accountInfo.getAccountNo());
		goldenBeanDetail.setGoldenBeanBalance(accountInfo.getGoldenBean());
		goldenBeanDetail.setGoldenBeanAccount(goldBeanNum);
		goldenBeanDetail.setGoldenBeanType(GoldenBeanType.recharge);
		goldenBeanDetail.setState(State.income);
		goldenBeanDetail.setTime(DateUtils.getCurrent());
		goldenBeanDetail.setOutTradeNo(outTradeNo);
		goldenBeanDetail.setComment("充值成功");
		goldenBeanDetail.setHesitatePeriodState(HesitatePeriodState.Hesitated);
		goldenBeanDetail.setModelStatus(ModelStatus.add);
		
		
		return accountInfoService.applyChange(accountInfo) && this.applyChange(accountRecharge) && goldenBeanDetailService.applyChange(goldenBeanDetail);
	}



}
